<template>
  <div class="login-in">
    <div class="login">
      <div class="login-head">
        <span>帐号登录</span>
      </div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="username">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import LoginLogo from '../components/LoginLogo'
import mixin from '../mixins'
import {loginIn} from '../api/index'

export default {
  name: 'login-in',
  mixins: [mixin],
  components: {
    LoginLogo
  },
  data() {
    return {
      ruleForm: {
        username: '',
        password: null
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '密码长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    this.changeIndex('登录')
  },
  methods: {
    changeIndex(value) {
      this.$store.commit('setActiveName', value)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this;
          loginIn(this.ruleForm)
            .then(res => {
              if (res.data.code === 200) {
                _this.$message.success('登陆成功')
                _this.setUserMsg(res.data.data)
                let token = res.headers['authorization']
                _this.$store.commit('setLoginIn', true)
                _this.$store.commit('setToken', token)
                setTimeout(function () {
                  _this.changeIndex('首页')
                  _this.$router.push('/')
                }, 2000)
              } else {
                _this.$message.error(res.data.msg)
              }
            })
            .catch(failResponse => {
              _this.$message.error(failResponse.data.msg)
            })
        } else {
          return false;
        }
      });
    },
    setUserMsg(item) {
      this.$store.commit('setUserId', item.id)
      this.$store.commit('setUsername', item.username)
      this.$store.commit('setIndustry', item.industry)
      this.$store.commit('setDescription', item.description)
      this.$store.commit('setQQ', item.qq)
      this.$store.commit('setWechat', item.wechat)
      this.$store.commit('setIsAdmin', !!item.isAdmin)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }

  }
}
</script>

<style lang="scss" scoped>
@import '../assets/css/login-in.scss';
</style>
